﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using PetroMentor.Service.Entities;
using Microsoft.Practices.Prism.Commands;

namespace HSE.Common
{
     [System.ComponentModel.DesignTimeVisible(false)]
    public partial class TelerikGridView : UserControl
    {
        public TelerikGridViewModel viewmodel;

        public delegate void PageChangedHandler(int pagenumber);
        public event PageChangedHandler   PageChanged;


        public delegate void EditHandler(object obj);
        public event EditHandler EditClicked;

        public TelerikGridView()
        {
            InitializeComponent();

            viewmodel = new TelerikGridViewModel();

            viewmodel.EditCommand = new DelegateCommand<object>(EditCommand_Execute);
            this.DataContext = viewmodel;
        }

        public void EditCommand_Execute(object obj)
        {
            if (this.EditClicked != null)
                this.EditClicked(obj);

        }

        public void Binding<T>(CommonQueryInfo<T> info)
        {
            pagenumlst.SelectionChanged -= new System.Windows.Controls.SelectionChangedEventHandler(pagenumlst_SelectionChanged);
            List<int> lst = new List<int>();
            for(int i=1;i<=info.PageCount;i++)
            {
                lst.Add(i);
            }
            viewmodel.Pages = lst;
            viewmodel.CurrentPage = info.PageNumber;
            this.gridview.ItemsSource = info.ResultInfos;
            pagenumlst.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(pagenumlst_SelectionChanged);
        }

        void pagenumlst_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (this.PageChanged != null)
            {
                this.PageChanged(viewmodel.CurrentPage);
            }
        }

        public void Init(GridMode mode, List<GridViewColumn> Columns)
        {
            this.gridview.Columns.Clear();
            if (mode == GridMode.Edit)
            {
                this.gridview.SelectionMode = System.Windows.Controls.SelectionMode.Extended;
                this.gridview.SelectionUnit = Telerik.Windows.Controls.GridView.GridViewSelectionUnit.FullRow;
                this.gridview.Columns.Add(new GridViewSelectColumn() { HeaderCellStyle = (Style)this.Resources["gridviewHeaderCell"], CellStyle = (Style)this.Resources["gridviewCell"] });
                viewmodel.EditVisibility = System.Windows.Visibility.Visible;
            }
            else
            {
                this.gridview.SelectionMode = System.Windows.Controls.SelectionMode.Single;
                this.gridview.SelectionUnit = Telerik.Windows.Controls.GridView.GridViewSelectionUnit.FullRow;

                viewmodel.SelectionMode = Telerik.Windows.Controls.SelectionMode.Single;
                viewmodel.EditVisibility = System.Windows.Visibility.Collapsed;
            }
              

            foreach (GridViewColumn column in Columns)
            {
                column.HeaderCellStyle = this.Resources["gridviewHeaderCell"] as Style;
                column.CellStyle = this.Resources["gridviewCell"] as Style;
                this.gridview.Columns.Add(column);
            }
        }

        public void InitColumn(List<GridViewColumn> Columns)
        {
            this.gridview.Columns.Clear();
             
            foreach(GridViewColumn column in Columns)
            {
                column.HeaderCellStyle = this.Resources["gridviewHeaderCell"] as Style;
                column.CellStyle = this.Resources["gridviewCell"] as Style;
                this.gridview.Columns.Add(column);
            }
           
        }

        private void gridview_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            this.viewmodel.SelectItems = this.gridview.SelectedItems;
        }

        
    }
}
